from collections import ChainMap
values = ChainMap()
values['x'] = 1
# new_child()在原字典上添加新的字典
values = values.new_child()
values['x'] = 2
values = values.new_child()
values['x'] = 3
print(values)
print(values['x'])
# 除去第一个位置的数
values = values.parents
print(values['x'])
values = values.parents
print(values['x'])
print(values)

# update()方法将两个字典合并
a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }
merged = dict(b)
merged.update(a)
print(merged)
a['x'] = 13
# 创建了一个新的字典,原字典改变,新字典不变
print(merged['x'])


a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }
# ChainMap使用原来的字典,不会产生新字典
merged = ChainMap(a, b)
print(merged['x'])
a['x'] = 42
print(merged['x']) # Notice change to merged dicts

